﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using NLBS.Data;
using NLBS.Entity;
using System.Text;
using NLBS.Web;
using NLBS.Web.UI;
using NLBS.Data.Business;
using NLBS.Views;

namespace NLBS.Control
{
    public partial class userDetails : BaseUserControl, IUserDetailView
    {
        #region Presenter

        private UserDetailPresenter _presenter;

        public UserDetailPresenter Presenter
        {
            get
            {
                
                if (this._presenter == null)
                    _presenter = new UserDetailPresenter();
                this._presenter.View = this;
                return _presenter;
            }

        }
        private void PresenterInit()
        {
            if (!this.IsPostBack)
            {
                this.Presenter.OnViewInitialized();
            }
            this.Presenter.OnViewLoaded();
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            PresenterInit();

            if (!string.IsNullOrEmpty(errorMsg))
            {
                ShowError(errorMsg);
                return;
            }
            BindUserProfile();
        }

        private void BindUserProfile()
        {
            string act = RequestWrapper.GetQueryString("act");
            if (String.IsNullOrEmpty(act))
            {
                userProfile.Visible = true;
                userDetailRP.DataSource = userList;
                userDetailRP.DataBind();
            }
            else if (act == "edit")
            {
                userEdit.Visible = true;
                editDetailRP.DataSource = userList;
                editDetailRP.DataBind();
            }

        }

        #region event

        protected override void OnInit(EventArgs e)
        {
            userDetailRP.ItemDataBound += new RepeaterItemEventHandler(userDetailRP_ItemDataBound);
            editDetailRP.ItemDataBound += new RepeaterItemEventHandler(userEditDetailRP_ItemDataBound);
            base.OnInit(e);
        }

        void userDetailRP_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //viewContent
            User viewUser=(User)e.Item.DataItem;
            Literal content = (Literal)e.Item.FindControl("viewContent");
            content.Text = RenderViewTable(viewUser);
        }

        void userEditDetailRP_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                User userItem = (User)e.Item.DataItem;

                Literal editUserContent = (Literal)e.Item.FindControl("editUserContent");
                editUserContent.Text = RenderEditTable(userItem);
                for(int i=1;i<=3;i++)
                {
                    HtmlInputRadioButton gender = (HtmlInputRadioButton)e.Item.FindControl("gender"+i);
                    if (gender.Value == userItem.Gender.ToString())
                    {
                        gender.Checked = true;
                    }
                }
                HtmlPanel securityCodePanel = (HtmlPanel)e.Item.FindControl("securityCodePanel");
                if (BlogInfo.EnableSecurityCode != 1)
                {
                    securityCodePanel.Visible = false;
                }


                //
            }
        }

        #endregion

        #region viewUser

        private string RenderViewTable(User item)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table cellpadding=\"2\" cellspacing=\"1\">");
            this.RenderUserViewRow("用户名", getViewName(item), sb);
            this.RenderUserViewRow("用户群组", item.Group.Name.ToString(), sb);
            this.RenderUserViewRow("统计数据", getStats(item), sb);
            this.RenderUserViewRow("性别", TransGender(item.Gender), sb);
            if (!item.HideEmail)
            this.RenderUserViewRow("email", item.Email, sb);
            this.RenderUserViewRow("主页", item.Homepage, sb);
            this.RenderUserViewRow("最后访问", item.LastVisit.ToString(), sb);
            if (BlogInfo.CurrentUser.GroupID == 1)
            {
            this.RenderUserViewRow("IP", item.IP, sb);
            }
            sb.Append("</table");
            return sb.ToString();
        }
        public void RenderUserViewRow(string cellName, string content, StringBuilder sb)
        {
            sb.Append("<tr>");
            sb.AppendFormat("<td class=\"formbox-rowheader\" width=\"120\">{0}:</td>", cellName);
            sb.AppendFormat("<td class=\"formbox-content\">{0}</td>", content);
            sb.Append("</tr>");

        }

        private string getViewName(User viewUser)
        {

            if (BlogInfo.CurrentUser != null)
            {
                if (BlogInfo.CurrentUser.GroupID == 1 || BlogInfo.CurrentUser.UserID == viewUser.UserID)
                {
                    return string.Format("{0} &nbsp;&nbsp; <a onclick=\"NLBSUser.getUserEditDetail('{1}')\" href=\"#\">[修改资料]</a>",
                viewUser.UserName, viewUser.UserID);
                }
            }
            return viewUser.UserName;
        }

        private string getStats(User viewUser)
        {
            string stats = string.Format("<a href=\"#\" onclick=\"{0}\" title=\"{1}\">日志: {2}</a>&nbsp;&nbsp;",
              urlFormats.UserArticle(viewUser.UserID),"查看该用户的所有日志",viewUser.ArticleCount);
          stats += string.Format("<a href=\"#\" onclick=\"{0}\" title=\"{1}\">评论: {2}</a>",
             urlFormats.UserComment(viewUser.UserID), "查看该用户的所有评论", viewUser.CommentCount);
          return stats; 
        }

        public string TransGender(int genderID)
        {
            string transStr = string.Empty;
            switch (genderID)
            {
                case 0:
                    transStr = "保密";
                    break;
                case 1:
                    transStr = "男";
                    break;
                case 2:
                    transStr = "女";
                    break;
            }
            return transStr;
        }
        #endregion

        #region editUser

        private string RenderEditTable(User editUser)
        {
            StringBuilder sb = new StringBuilder();
            this.RenderUserViewRow("用户名", RenderEditUserName(editUser), sb);
            this.RenderUserViewRow("用户群组", RenderEditGroup(editUser), sb);
            return sb.ToString();
        }


        private string RenderEditUserName(User editUser)
        {
            StringBuilder sb=new StringBuilder();
            sb.Append(editUser.UserName);
            if (BlogInfo.CurrentUser != null)
            {
                if (BlogInfo.CurrentUser.GroupID == 1 && BlogInfo.CurrentUser.UserID != editUser.UserID)
                {
                    sb.AppendFormat(" &nbsp;&nbsp; <a href=\"#\" onclick=\"NLBSUser.deleteUser('{0}')\">", editUser.UserID);
                    sb.Append("<img src=\"styles/default/images/icon_del.gif\" title=\"删除\" /></a>");
                }
            }
            return sb.ToString();                       
        }
        private string RenderEditGroup(User editUser)
        {
            User currentUser = BlogInfo.CurrentUser;
            if (currentUser.GroupID == 1)
            {
                HtmlSelect group = new HtmlSelect();
                group.ID = "groupID";
                group.Name = "groupID";
                foreach (UserGroup groupItem in UserGroupData.GetUserGroup())
                {
                    ListItem item = new ListItem(groupItem.Name, groupItem.GroupID.ToString());
                    if (groupItem.GroupID == editUser.GroupID)
                    {
                        item.Selected = true;
                    }
                    group.Items.Add(item);
                }
                return ViewManager.RenderView(group);
            }
            else
            {
                return editUser.GroupName;
            }
            return string.Empty;
        }


        #endregion

        #region IUserDetailView Members

        public int userID
        {
            get
            {
                return RequestWrapper.GetQueryInt("id");
            }

        }

        private IList<User> _userList;
        public IList<User> userList
        {
            get
            {
                return _userList;
            }
            set
            {
                _userList=value;
            }
        }

        private string _errorMsg;
        public string errorMsg
        {
            get
            {
                return _errorMsg;
            }
            set
            {
                _errorMsg=value;
            }
        }

        #endregion
    }
}